package com.bw.paper.mvp.base.view;


import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import com.bw.paper.mvp.base.presenter.IPresenter;
import com.bw.paper.utils.ToastUtils;
import com.bw.paper.widget.LoadDialog;

import javax.inject.Inject;

public  abstract  class BaseFragment<P extends IPresenter> extends Fragment
        implements IView,IFragment {
    @Inject
    protected  P p;
    protected LoadDialog loadDialog;

    private   View view;
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {

         view = inflater.inflate(bindlayout(), container, false);
        loadDialog = new LoadDialog(getContext());
        initView();
        inject();
        getLifecycle().addObserver(p);
        initData();
        return view;

    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        getLifecycle().removeObserver(p);
    }

    @Override
    public void startPage(Bundle bundle, Class clazz) {
        Intent intent = new Intent(getActivity(), clazz);
        if (bundle!=null)
            intent.putExtras(bundle);
       getActivity().startActivity(intent);
    }

    @Override
    public void showDialog() {
        loadDialog.show();
    }

    @Override
    public void hideDialog() {
        loadDialog.dismiss();
    }

    @Override
    public void showMsg(String msg) {
        ToastUtils.showMsg(msg);
    }

    public void startPage(Class clazz){
        startPage(null,clazz);
    }

    protected <T extends View> T f(int id){
        return view.findViewById(id);
    }
}
